#pragma once
#include "Vec2.hpp"
#include <algorithm>
class Box
{
public:
   Box()
      : min_x(0), min_y(0), max_x(0), max_y(0)
   {
   }
   Box(float aMinX,
       float aMinY,
       float aMaxX,
       float aMaxY)
   :  min_x(aMinX),
      min_y(aMinY),
      max_x(aMaxX),
      max_y(aMaxY)
   {}
   Box(const Vec2& a, const Vec2& b)
   :  min_x(a.x),
      min_y(a.y),
      max_x(b.x),
      max_y(b.y)
   {
      if (min_x > max_x) std::swap(min_x, max_x);
      if (min_y > max_y) std::swap(min_y, max_y);
   }

   const float& operator[](int aIndex) const { return values[aIndex]; }
   float& operator[](int aIndex) { return values[aIndex]; }
   Box operator/(float aD) const
   {
      if (aD > 0)
      {
         return Box(min_x / aD, 
                    min_y / aD,
                    max_x / aD,
                    max_y / aD);
      }
      else
      {
         return Box(max_x / aD, 
                    max_y / aD,
                    min_x / aD,
                    min_y / aD);
      }
   }
   Box operator*(float aM) const
   {
      if (aM > 0)
      {
         return Box(min_x * aM, 
            min_y * aM,
            max_x * aM,
            max_y * aM);
      }
      else
      {
         return Box(max_x * aM, 
            max_y * aM,
            min_x * aM,
            min_y * aM);
      }
   }
   Box operator+(float aM) const
   {
      return Box(min_x + aM, 
         min_y + aM,
         max_x + aM,
         max_y + aM);
   }
   float GetDimension(int aIndex) const { return values[aIndex+2] - values[aIndex]; }
   float GetArea() const { return GetDimension(0) * GetDimension(1); }
   bool  Contains(const Vec2& aVec) const
   {
      return min_x < aVec.x && max_x > aVec.x && min_y < aVec.y && max_y > aVec.y;
   }
   Box operator+(const Box& aRHS) const
   {
      Box rv;
      rv.min_x = std::min(min_x, aRHS.min_x);
      rv.min_y = std::min(min_y, aRHS.min_y);
      rv.max_x = std::max(max_x, aRHS.max_x);
      rv.max_y = std::max(max_y, aRHS.max_y);
      return rv;
   }
   Box operator-(const Box& aRHS) const
   {
      Box rv;
      rv.min_x = std::max(min_x, aRHS.min_x);
      rv.min_y = std::max(min_y, aRHS.min_y);
      rv.max_x = std::min(max_x, aRHS.max_x);
      rv.max_y = std::min(max_y, aRHS.max_y);
      return rv;
   }
   union
   {
      float values[4];
      struct
      {
         float min_x;
         float min_y;
         float max_x;
         float max_y;
      };
   };
};


